import pandas as pd
from sqlalchemy import create_engine


def output():
    # 根据实际情况修改服务器地址、数据库名、用户名和密码
    server = 'DESKTOP-2NR5UU8'
    username = 'abc'
    password = '123456'
    database = 'python'
    # 使用 utf-8 或 gbk 字符集，根据实际情况选择
    connection_str = f'mssql+pyodbc://{username}:{password}@{server}/{database}?driver=ODBC+Driver+17+for+SQL+Server&charset=utf-8'
    engine = create_engine(connection_str)
    # 读取数据
    df = pd.read_sql('SELECT * FROM lianjia_Last_version ', engine)
    # 将DataFrame写入Excel文件
    excel_filename = 'information.xlsx'
    df.drop_duplicates(inplace=True)
    df.to_excel(excel_filename, index=False, engine='openpyxl')  # index=False表示不将DataFrame的索引写入Excel
    print(f"数据已成功写入{excel_filename}文件！")